package com.example.transvipm;

import java.util.ArrayList;

import com.example.Objetos.ObjRes;
import com.example.Objetos.ObjRes2;
import com.example.Objetos.ObjSalida;
import com.example.dbclass.ConexionWS;
import com.example.dbclass.ConnectionDetector;
import com.example.dbclass.DBHelper;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Salidas extends Activity {

	private ListView lista;
	private DBHelper dbres;
	private TextView num;
	private String Numero;
	private ArrayList<String> plan = new ArrayList<String>();
	private ArrayAdapter<String> adaptador;

	private TextView textName;
	private TextView contName;
	private TextView textRut;
	private TextView contRut;
	private TextView textTkt;
	private TextView contTkt;
	private TextView textOri;
	private TextView contOri;
	private TextView textDest;
	private TextView contDest;
	private TextView textHora;
	private TextView contHora;
	private TextView textVeh;
	private TextView contVeh;

	private int movil;
	private ImageButton v_menu;
	private Vibrator vib;

	ListView listView;
	SalidasAdapter myadapter;
	ArrayList<String> arr_cars = new ArrayList<String>();
	AlertDialog.Builder alertDialogBuilder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reservas);
		textName = (TextView) findViewById(R.id.textName);

		num = (TextView) findViewById(R.id.numero_movil);
		Bundle Datos = getIntent().getExtras();
		Numero = Datos.getString("Numero");
		num.setText(Numero);
		movil = Integer.parseInt(Numero);
		vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		v_menu = (ImageButton) findViewById(R.id.Volver_menu);

		dbres = new DBHelper(getApplicationContext());

		v_menu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				vib.vibrate(300);
				onBackPressed();
			}
		});

		v_menu.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {

					v_menu.setImageResource(R.drawable.menu_pressed);

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					v_menu.setImageResource(R.drawable.menu);

				}

				return false;
			}
		});
		ArrayList<ObjSalida> lista_salidas = new ArrayList<ObjSalida>();

		listView = (ListView) findViewById(R.id.ListaServicios);

		try {

			lista_salidas = dbres.ObtenerSalidas();

		} catch (SQLiteDatabaseLockedException e) {
			Toast.makeText(Salidas.this,
					"Error al Realizar la Consulta, Intente Nuevamente",
					Toast.LENGTH_LONG).show();
		}

		myadapter = new SalidasAdapter(Salidas.this, lista_salidas);
		/*
		 * View header = getLayoutInflater().inflate(R.layout.header, null);
		 * listView.addHeaderView(header);
		 */

		listView.setAdapter(myadapter);

	}

	// if you want to add functionality to your objects in the rows, you need to
	// do it in your adapter class.
	// Now we are going to let the user delete a row from the list buy clicking
	// on the button (ld. lent)

	public class SalidasAdapter extends BaseAdapter {

		ArrayList<ObjSalida> salidas = new ArrayList<ObjSalida>();
		public Activity context;

		public LayoutInflater inflater;

		public SalidasAdapter(Activity context, ArrayList<ObjSalida> salidas) {
			super();

			this.context = context;
			this.salidas = salidas;

			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return salidas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public class ViewHolder {
			ImageView image;
			TextView Salida;
			TextView PrimeraRecogida;
			TextView LlegadaAeropuerto;
			TextView NumPasajeros;
			ImageButton btn;
			LinearLayout row;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub

			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.cont_lista_res, null);

				holder.image = (ImageView) convertView
						.findViewById(R.id.imageTipPago);
				holder.Salida = (TextView) convertView
						.findViewById(R.id.textSalida);
				holder.PrimeraRecogida = (TextView) convertView
						.findViewById(R.id.textPrimeraRecogida);
				holder.LlegadaAeropuerto = (TextView) convertView
						.findViewById(R.id.textLlegadaAeropuerto);
				holder.NumPasajeros = (TextView) convertView
						.findViewById(R.id.textNumPasajeros);
				holder.btn = (ImageButton) convertView
						.findViewById(R.id.buttonDetalle);
				holder.row = (LinearLayout) convertView.findViewById(R.id.row);
				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			holder.Salida.setText(String.valueOf(salidas.get(position)
					.getSalPapelet()));

			holder.PrimeraRecogida.setText(salidas.get(position)
					.getFecha_primera()
					+ " "
					+ salidas.get(position).getHora_primera());

			holder.LlegadaAeropuerto.setText(salidas.get(position)
					.getFecha_aeropuerto()
					+ " "
					+ salidas.get(position).getHora_aeropuerto());

			holder.NumPasajeros.setText(String.valueOf(salidas.get(position)
					.getTotal()));

			if (salidas.get(position).getPago().equals("1")) {
				holder.image.setImageDrawable(getResources().getDrawable(
						R.drawable.control1));
			} else {
				holder.image.setImageDrawable(getResources().getDrawable(
						R.drawable.control0));
			}

			holder.btn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					
					vib.vibrate(300);

					Reservas(v, Numero, String.valueOf(salidas.get(position)
							.getSalPapelet()), salidas.get(position)
							.getFecha_primera()
							+ " "
							+ salidas.get(position).getHora_primera(), salidas
							.get(position).getFecha_aeropuerto()
							+ " "
							+ salidas.get(position).getHora_aeropuerto(),
							String.valueOf(salidas.get(position).getTotal()),
							String.valueOf(salidas.get(position).getPago()));
					

					Recepcionado tarea3 = new Recepcionado();
					tarea3.execute(String
							.valueOf( String.valueOf(salidas.get(position)
									.getSalPapelet())));
				}
			});

			holder.btn.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {

					if (event.getAction() == MotionEvent.ACTION_DOWN) {

						holder.btn.setImageResource(R.drawable.detalle_pressed);

					} else if (event.getAction() == MotionEvent.ACTION_UP) {
						holder.btn.setImageResource(R.drawable.detalle);

					}

					return false;
				}
			});

		
			return convertView;

		}

	}

	public void onBackPressed() {
		super.onBackPressed();
	};

	public void Reservas(View v, String Numero, String papelet,
			String pRecogida, String lAeropuerto, String pax, String pago) {
		Intent act = new Intent(this, ListadoReservas.class);
		act.putExtra("Numero", Numero);
		act.putExtra("Papelet", papelet);
		act.putExtra("PRecogida", pRecogida);
		act.putExtra("LAeropuerto", lAeropuerto);
		act.putExtra("Pax", pax);
		act.putExtra("Pago", pago);
		startActivity(act);

	}
	
	private class Recepcionado extends AsyncTask<String, Integer, Boolean> {

		String res;
		@Override
		protected Boolean doInBackground(String... params) {
			
			res = params[0];
			ConnectionDetector con = new ConnectionDetector(getBaseContext());
			// check for Internet status
			if (con.isConnectingToInternet()) {
				ConexionWS conexion = new ConexionWS();
				conexion.Recepcionado(num.getText().toString(), res);			
				return true;

			} else {
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result) {

			if (result) {
				
				Toast.makeText(Salidas.this,
						"Recepcionado",
						Toast.LENGTH_LONG).show();


			}else
			{
				Toast.makeText(Salidas.this,
						"Error al Recepcionar, Intente Nuevamente",
						Toast.LENGTH_LONG).show();
			}
		}
	}

}